//
//  RegisterWithPhoneNoController.m
//  陶瓷
//
//  Created by 王戈 on 15/6/22.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "RegisterWithPhoneNoController.h"
#import "RegisterController.h"

@interface RegisterWithPhoneNoController () <UITextFieldDelegate>

@property (nonatomic, weak) IBOutlet UITextField *textField;
@property (nonatomic, weak) IBOutlet UITextField *verifyCodeTF;
@property (nonatomic, strong) NSString *verifycode;
@property (nonatomic, strong) UIButton *cover;
@property (nonatomic, weak) IBOutlet UIButton *sendButton;
@property (nonatomic, weak) IBOutlet UIButton *verifyCodeButton;

@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic, assign) int stopTime;


@end

@implementation RegisterWithPhoneNoController


- (void)showCurrentRemainSecond
{
    int nowSince = [[NSDate date] timeIntervalSince1970];
    NSString *str = [NSString stringWithFormat:@"重新获取%ds",self.stopTime - nowSince];
    self.verifyCodeButton.backgroundColor = [UIColor lightGrayColor];
    [self.verifyCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; ;
    [self.verifyCodeButton setTitle:str forState:UIControlStateNormal];
}
- (void)setCountDownState
{
    self.stopTime = [[NSDate date] timeIntervalSince1970];
    self.stopTime += 90;
    self.verifyCodeButton.enabled = NO;
    [self showCurrentRemainSecond];
    [self stopTimer];
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}
- (void)countDownAction
{
    if ([[NSDate date] timeIntervalSince1970] > self.stopTime) {
        [self resumeVerifyButtonStatus];
    } else {
        [self showCurrentRemainSecond];
    }
}

- (void)resumeVerifyButtonStatus
{
    [self stopTimer];
    self.verifyCodeButton.enabled = YES;
    [self.verifyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.verifyCodeButton.backgroundColor = [UIColor whiteColor];
    [self.verifyCodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (void)stopTimer
{
    if (self.countDownTimer != nil) {
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
    }
}

- (void)dealloc
{
    [self stopTime];
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    _textField.delegate = self;
    _textField.keyboardType = UIKeyboardTypeNumberPad;
//    self.title = @"注册";
    
    _verifyCodeButton.layer.cornerRadius = 10;
    _verifyCodeButton.layer.borderWidth = 0.5;
    _verifyCodeButton.layer.borderColor = [UIColor blueColor].CGColor;
    [_verifyCodeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.verifyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    _sendButton.layer.cornerRadius = 10;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"注册"];
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)nestStep {
    
    if (_textField.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入手机号"];
        return;
    }
    if (_verifyCodeTF.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入验证码"];
        return;
    }
    RegisterController *rVC = [[RegisterController alloc] init];
    rVC.verify = _verifyCodeTF.text;
    rVC.phoneNum = _textField.text;
    [self.navigationController pushViewController:rVC animated:YES];
    
}

- (IBAction)btnTouchUpInside:(UIButton *)verifyCodeBtn
{
    if (_textField.text.length == 0) {
        [STAlertView st_promptAlertWithMessage:@"请填入手机号"];
        return;
    }
    [SVProgressHUD showWithStatus:@"正在注册 请稍候"];
    [self.view addSubview:self.cover];
    [self setCountDownState];
    [[TCNetworkTool sharedNetTool] verifyCodeWithCallNumber:_textField.text andSuccessBlocks:^(NSString *successMessage) {
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
        [SVProgressHUD dismiss];
        [self.cover removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self nestStep];
    return YES;
}

@end
